//
//  SSCheckoutCleanViewController.swift
//  拾舍清洁
//
//  Created by sky.wei on 2023/2/22.
//

import UIKit

import SwiftyJSON


class SSCheckoutCleanViewController: UIViewController {
    var type:String = "1"
    
    ///数据
    var items: Array<ListItemCombo> = []
    
    @IBOutlet weak var tableView: UITableView!
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        tableView.reloadData()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(UINib(nibName: "SSHomeTableViewCell", bundle: Bundle.init(for: SSHomeTableViewCell.self)), forCellReuseIdentifier: "SSHomeTableViewCell")
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        getListNetwork()
    }
    //MARK: -networing
    //list
    func getListNetwork() {
        let param = [
                    "parentId": "2",
                    "type": type
        ] as [String:AnyObject]
        
        SSProductRequest.getProductRequest(parameters: param) { items in
            self.items = items
            self.tableView.reloadData()
//            self.getBannerNetwork()
        }
    }

}



extension SSCheckoutCleanViewController: UITableViewDelegate, UITableViewDataSource
{
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.items.count
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 42
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        var title = ""
        if self.items.count > 0 {
            title = (self.items[section] ).name
        }
        let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 42))
        view.backgroundColor = UIColor.white
        let label = UILabel()
        label.text = title
        label.font = UIFont.systemFont(ofSize: 18, weight: .bold)
        label.textColor = UIColor.black
        view.addSubview(label)
        label.snp.makeConstraints { make in
            make.width.equalTo(ScreenWidth - 32)
            make.height.equalTo(26)
            make.centerY.equalTo(view.frame.size.height/2)
            make.left.equalTo(16)
        }
        
        return view
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items[section].listCombos.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SSHomeTableViewCell") as! SSHomeTableViewCell
        cell.setData(self.items[indexPath.section].listCombos[indexPath.row] as! ListCombo)
        cell.setType(type: "2")
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 170
    }
}
